package net.chrisrichardson.ec2deployer.core

import groovy.mock.interceptor.MockFor
import net.chrisrichardson.ec2deployer.cluster.EbsVolume

class EC2ServerTest extends GroovyTestCase {

    MockFor mockEc2
    AWSProperties awsProperties
    MockFor mockSsh

    public void testSarMonitor() {
        def ec2Server = new EC2Server()
        assertNotNull ec2Server.sarMonitor
    }

    public void setUp() {
        mockEc2 = new MockFor(EC2.class)
        def props = new Properties()
        awsProperties = new AWSProperties(props)
        mockSsh = new MockFor(Ssh)
    }

    public void testWaitUntilRunning() {
        mockSsh.demand.waitUntilSshAvailable {}

        mockEc2.use {
            mockEc2.demand.getSsh { -> new Ssh() }
            mockEc2.demand.getStartupScript{ null }
            
            mockSsh.use {
                    def ec2 = new EC2(awsProperties)
                    def ssh = new Ssh()

                    def ec2Server = new EC2Server(ec2, "i-34343", "ami-XYZ", "pending", "foo")
                    ec2Server.updateStatus("running", "foo.bar.com", "internal.ip")
                    ec2Server.waitUntilRunning()
            }

        }

    }

    public void testAttachVolume() {
        EbsVolume volume = EbsVolume.existingVolume("vol-1121", "/dev/sdq")
        mockEc2.demand.executeRequest { params ->
            assertEquals (["Action" : "AttachVolume", "VolumeId" : "vol-1121", "InstanceId": "i-34343", "Device":"/dev/sdq"], params)
        }
        mockEc2.use {
            def ec2 = new EC2(awsProperties)
            def ec2Server = new EC2Server(ec2, "i-34343", "ami-XYZ", "pending", "foo")
            ec2Server.attachEbsVolume volume
        }
    }

    public void testCreateVolume() {
        def response = """<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2008-05-05">
  <volumeId>vol-4d826724</volumeId>
  <size>800</size>
  <status>creating</status>
  <createTime>2008-05-07T11:51:50.000Z</createTime>
  <zone>us-east-1a</zone>
  <snapshotId></snapshotId>
</CreateVolumeResponse>
"""        
        EbsVolume volume = EbsVolume.newVolume(11, "/dev/sdq")
        mockEc2.demand.executeRequest { params ->
            assertEquals (["Action" : "CreateVolume", "Size" : "11", "AvailabilityZone": "foo"], params)
            def parser = new XmlParser(false, false)
            return parser.parse(new StringReader(response))
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "creating"
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "available"
        }

        mockEc2.use {
            def ec2 = new EC2(awsProperties)
            def ec2Server = new EC2Server(ec2, "i-34343", "ami-XYZ", "pending", "foo")
            ec2Server.createEbsVolume volume
        }
    }

    public void testCreateVolumeFromSnapshot() {
        def response = """<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2008-05-05">
  <volumeId>vol-4d826724</volumeId>
  <size>800</size>
  <status>creating</status>
  <createTime>2008-05-07T11:51:50.000Z</createTime>
  <zone>us-east-1a</zone>
  <snapshotId></snapshotId>
</CreateVolumeResponse>
"""
        EbsVolume volume = EbsVolume.newVolumeFromSnapshot("snap-1234", 11, "/dev/sdq")
        mockEc2.demand.executeRequest { params ->
            assertEquals (["Action" : "CreateVolume", "Size" : "11", "AvailabilityZone": "foo",  "SnapshotId" : "snap-1234"], params)
            def parser = new XmlParser(false, false)
            return parser.parse(new StringReader(response))
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "creating"
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "available"
        }

        mockEc2.use {
            def ec2 = new EC2(awsProperties)
            def ec2Server = new EC2Server(ec2, "i-34343", "ami-XYZ", "pending", "foo")
            ec2Server.createEbsVolume volume
        }
    }

    public void testCreateVolumeFromSnapshotWithSameSize() {
        def response = """<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/2008-05-05">
  <volumeId>vol-4d826724</volumeId>
  <size>800</size>
  <status>creating</status>
  <createTime>2008-05-07T11:51:50.000Z</createTime>
  <zone>us-east-1a</zone>
  <snapshotId></snapshotId>
</CreateVolumeResponse>
"""
        EbsVolume volume = EbsVolume.newVolumeFromSnapshot("snap-1234", null, "/dev/sdq")
        mockEc2.demand.executeRequest { params ->
            assertEquals (["Action" : "CreateVolume",  "AvailabilityZone": "foo",  "SnapshotId" : "snap-1234"], params)
            def parser = new XmlParser(false, false)
            return parser.parse(new StringReader(response))
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "creating"
        }

        mockEc2.demand.getStatusOfEbsVolume { ebsVolume ->
            assertEquals "vol-4d826724", ebsVolume.volumeId
            "available"
        }

        mockEc2.use {
            def ec2 = new EC2(awsProperties)
            def ec2Server = new EC2Server(ec2, "i-34343", "ami-XYZ", "pending", "foo")
            ec2Server.createEbsVolume volume
        }
    }
}